class Solution(object):
    def maximumNumber(self, num, change):
        num = list(num)
        flag = False
        for i, c in enumerate(num):
            if change[int(c)] > int(c):
                num[i] = str(change[int(c)])
                flag = True
            else:
                if change[int(c)] < int(c):
                    if flag:
                        return ''.join(num)
        return ''.join(num)

